package com.lf.sys.util;

import java.util.ArrayList;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;

public class MailFolder {
	private Folder folder;
	private int deletedCount;
	private int messageCount;
	private int newCount;
	private int unreadCount;
	private int state;
	private String error;
	private ArrayList<MailContent> mailContent;
	private Message[] messages;

	public MailFolder(Folder folder) {
		this.folder = folder;
		this.init();
	}

	private void init() {
		this.mailContent = new ArrayList<MailContent>();
		if (!this.folder.isOpen()) {
			try {
				this.folder.open(Folder.READ_WRITE);
			} catch (MessagingException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				return;
			}
		}
		try {
			this.deletedCount = this.folder.getDeletedMessageCount();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			this.messageCount = this.folder.getMessageCount();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			this.newCount = this.folder.getNewMessageCount();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			this.folder.getUnreadMessageCount();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Folder getFolder() {
		return folder;
	}

	public void setFolder(Folder folder) {
		this.folder = folder;
	}

	public int getDeletedCount() {
		return deletedCount;
	}

	public void setDeletedCount(int deletedCount) {
		this.deletedCount = deletedCount;
	}

	public int getMessageCount() {
		return messageCount;
	}

	public void setMessageCount(int messageCount) {
		this.messageCount = messageCount;
	}

	public int getNewCount() {
		return newCount;
	}

	public void setNewCount(int newCount) {
		this.newCount = newCount;
	}

	public int getUnreadCount() {
		return unreadCount;
	}

	public void setUnreadCount(int unreadCount) {
		this.unreadCount = unreadCount;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public ArrayList<MailContent> getMailContent() {
		if (this.mailContent == null) {
			this.mailContent = new ArrayList<MailContent>();
		}
		Message[] message = this.getMessages();
		if (message != null) {
			for (int i = 0; i < message.length; i++) {
				this.mailContent.add(new MailContent(message[i]));
			}
		}
		return this.mailContent;
	}

	public void setMailContent(ArrayList<MailContent> mailContent) {
		this.mailContent = mailContent;
	}

	public Message[] getMessages() {
		if (!this.getFolder().isOpen()) {
			try {
				this.getFolder().open(Folder.READ_WRITE);
				this.setState(0);
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.state = 1;
				this.error = e.getMessage();
			}
		}
		if (this.getState() == 0) {
			try {
				this.setMessages(this.getFolder().getMessages());
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				this.state = 1;
				this.error = e.getMessage();
			}
		} else {
			this.messages = null;
		}
		return this.messages;
	}

	public void setMessages(Message[] messages) {
		this.messages = messages;
	}

}
